package icu.javayanglei.bilibili.community.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import icu.javayanglei.bilibili.community.service.BulletScreenService;
import icu.javayanglei.bilibili.domain.Video;
import icu.javayanglei.bilibili.domain.vo.PageVO;
import icu.javayanglei.bilibili.domain.vo.QueryBV;
import icu.javayanglei.bilibili.domain.vo.QueryBVDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yang
 * @version 1.0.0
 * @date 2022-01-11 02:59
 */
@Service
@Slf4j
public class BulletScreenServiceImpl implements BulletScreenService {
    @Override
    public List<String> BVList(String uid) {
        List<String> result = CollUtil.newArrayList();
        final QueryBV queryBV = new QueryBV();
        queryBV.setMid(uid);

        final JSONObject json = queryBv(queryBV);

        final JSONObject data = json.getJSONObject("data");


        final JSONObject page = data.getJSONObject("page");

        final PageVO pageVO = page.toBean(PageVO.class);
        final Integer calculationPn = pageVO.calculationPn();

        for (int i = PageVO.minPN; i <= calculationPn; i++) {
            queryBV.setPn(Convert.toStr(i));
            final JSONObject object = queryBv(queryBV);

            final JSONArray vList = object.getJSONObject("data").getJSONObject("list").getJSONArray("vlist");
            final List<String> bvid = vList.stream()
                    .map(JSONUtil::parseObj)
                    .filter(ObjectUtil::isNotNull)
                    .map(e -> e.getStr("bvid"))
                    .collect(Collectors.toList());
            result.addAll(bvid);
            Console.log("当前 list ", result);
        }

        return result;
    }

    @Override
    public Video queryByBV(String bvid) {
        final QueryBVDetail detail = new QueryBVDetail(bvid);
        final JSONObject jsonObject = queryBvDetail(detail);

        final JSONObject data = jsonObject.getJSONObject("data");

        final String cid = data.getStr("cid");
        final String pic = data.getStr("pic");
        final String title = data.getStr("title");
        Integer thousand = 1000;
        final Integer pubdate = data.getInt("pubdate");
        BigDecimal pubdateBig = NumberUtil.mul(pubdate, thousand);
        final Integer ctime = data.getInt("ctime");
        BigDecimal ctimeBig = NumberUtil.mul(ctime, thousand);
        final String desc = data.getStr("desc");


        final JSONObject stat = data.getJSONObject("stat");
        final Integer view = stat.getInt("view");
        final Integer danmaku = stat.getInt("danmaku");
        final Integer reply = stat.getInt("reply");
        final Integer favorite = stat.getInt("favorite");
        final Integer coin = stat.getInt("coin");
        final Integer share = stat.getInt("share");
        final Integer like = stat.getInt("like");
        final Integer dislike = stat.getInt("dislike");

        final Integer mid = data.getJSONObject("owner").getInt("mid");

        return Video.builder()
                .bvid(bvid)
                .cid(cid)
                .pic(pic)
                .title(title)
                .pubdate(LocalDateTimeUtil.of(pubdateBig.longValue()))
                .ctime(LocalDateTimeUtil.of(ctimeBig.longValue()))
                .desc(desc)
                .view(view.longValue())
                .danmaku(danmaku.longValue())
                .reply(reply.longValue())
                .favorite(favorite.longValue())
                .coin(coin.longValue())
                .share(share.longValue())
                .like(like.longValue())
                .dislike(dislike.longValue())
                .mid(Convert.toStr(mid))
                .build();
    }

    private JSONObject queryBvDetail(QueryBVDetail queryBV) {
        return query(queryBV, "https://api.bilibili.com/x/web-interface/view");
    }

    private JSONObject queryBv(QueryBV queryBV) {
        return query(queryBV, "https://api.bilibili.com/x/space/arc/search");
    }

    private JSONObject query(Object obj, String url) {
        final HttpRequest request = HttpRequest.get(url).form(BeanUtil.beanToMap(obj)).cookie("SESSDATA=26d97fcf%2C1656854927%2Cf3a04*11;");
        final HttpResponse response = request.executeAsync();
        if (response.isOk()) {
            final String body = response.body();
            return JSONUtil.parseObj(body);
        } else {
            throw new RuntimeException("没有响应！");
        }
    }
}
